package com.ellricky.baselib.adapter.course

import android.content.Intent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.andr.common.ui.adapter.BaseRecyclerAdapter
import com.andr.common.ui.adapter.SmartViewHolder
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.entity.new.WareLearn
import com.ellricky.baselib.ui.course.PrepareCourseActivity
import com.ellricky.baselib.ui.course.TYPE_REMARK

class CourseSelectAdapter(private val type: String) :
    BaseRecyclerAdapter<WareLearn>(R.layout.base_adapter_course_select) {

    override fun onBindViewHolder(vh: SmartViewHolder, p1: WareLearn, position: Int) {
        val ivAdd = vh.itemView.findViewById<ImageView>(R.id.iv_add)
        val tvView = vh.itemView.findViewById<TextView>(R.id.tv_view)
        vh.text(R.id.tvPosition, p1.ware_sort)
            ?.text(R.id.tvTitle, p1.ware_name)
        if (type == TYPE_REMARK) {
            try {
                if (p1.prepare_num > 0) {
                    ivAdd.visibility = View.GONE
                    tvView.visibility = View.VISIBLE
                    tvView.text = "已上传${p1.prepare_num}个文件\n点击查看"
                } else {
                    ivAdd.visibility = View.VISIBLE
                    tvView.visibility = View.GONE
                }
            }catch (e:Exception){
                e.printStackTrace()
            }

            ivAdd.setOnClickListener {
                val context = vh.itemView.context
                val intent = Intent(context, PrepareCourseActivity::class.java).apply {
                    putExtra(BaseConstant.KEY_ID, p1.ware_id)
                }
                context.startActivity(intent)
            }

            tvView.setOnClickListener {
                val context = vh.itemView.context
                val intent = Intent(context, PrepareCourseActivity::class.java).apply {
                    putExtra(BaseConstant.KEY_ID, p1.ware_id)
                }
                context.startActivity(intent)
            }
        } else {
            ivAdd.visibility = View.GONE
        }
    }
}